name: WebAssembly
on:
  push:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'
  pull_request:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'
  merge_group:
    paths-ignore:
      - 'doc/**'
      - '**/man'
      - '**.md'
      - '**.rdoc'
      - '**/.document'

concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.sha }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}

permissions:  # added using https://github.com/step-security/secure-workflows
  contents: read

jobs:
  make:
    strategy:
      matrix:
        entry:
#         # wasmtime can't compile non-optimized Asyncified binary due to locals explosion
#         - { name: O0-debuginfo, optflags: "-O0", debugflags: "-g", wasmoptflags: "-O1" }
#         - { name: O1,           optflags: "-O1", debugflags: ""  , wasmoptflags: "-O1" }
          - { name: O2,           optflags: "-O2", debugflags: ""  , wasmoptflags: "-O2" }
#         - { name: O3,           optflags: "-O3", debugflags: ""  , wasmoptflags: "-O3" }
#         # -O4 is equivalent to -O3 in clang, but it's different in wasm-opt
#         - { name: O4,           optflags: "-O3", debugflags: ""  , wasmoptflags: "-O4" }
#         - { name: Oz,           optflags: "-Oz", debugflags: ""  , wasmoptflags: "-Oz" }
      fail-fast: false
    env:
      RUBY_TESTOPTS: '-q --tty=no'
      GITPULLOPTIONS: --no-tags origin ${{github.ref}}
      WASI_SDK_VERSION_MAJOR: 14
      WASI_SDK_VERSION_MINOR: 0
      BINARYEN_VERSION: 109
      WASMTIME_VERSION: v0.33.0
    runs-on: ubuntu-20.04
    if: ${{ !contains(github.event.head_commit.message, '[DOC]') && !contains(github.event.pull_request.labels.*.name, 'Documentation') }}
    steps:
      - run: mkdir build
        working-directory:
      - name: git config
        run: |
          git config --global advice.detachedHead 0
          git config --global init.defaultBranch garbage
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
        with:
          path: src
      - name: Install libraries
        run: |
          set -ex
          sudo apt-get update -q || :
          sudo apt-get install --no-install-recommends -q -y ruby make autoconf git wget

          wasi_sdk_deb="wasi-sdk_${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}_amd64.deb"
          wget "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION_MAJOR}/${wasi_sdk_deb}"
          sudo dpkg -i "$wasi_sdk_deb"
          rm -f "$wasi_sdk_deb"

          mkdir build-sdk
          pushd build-sdk

          wasmtime_url="https://github.com/bytecodealliance/wasmtime/releases/download/${WASMTIME_VERSION}/wasmtime-${WASMTIME_VERSION}-x86_64-linux.tar.xz"
          wget -O - "$wasmtime_url" | tar xJf -
          sudo ln -fs "$PWD/wasmtime-${WASMTIME_VERSION}-x86_64-linux/wasmtime" /usr/local/bin/wasmtime

          binaryen_tarball="binaryen-version_${BINARYEN_VERSION}-x86_64-linux.tar.gz"
          binaryen_url="https://github.com/WebAssembly/binaryen/releases/download/version_${BINARYEN_VERSION}/${binaryen_tarball}"
          wget -O - "$binaryen_url" | tar xfz -
          sudo ln -fs "$PWD/binaryen-version_${BINARYEN_VERSION}/bin/wasm-opt" /usr/local/bin/wasm-opt
        working-directory: src
      - name: Set ENV
        run: |
          echo "MAKEFLAGS=-j$((1 + $(sysctl -n hw.activecpu)))" >> $GITHUB_ENV
          echo "WASI_SDK_PATH=/opt/wasi-sdk" >> $GITHUB_ENV
      - run: ./autogen.sh
        working-directory: src
      - name: Run configure
        run: |
          ../src/configure \
            --host wasm32-unknown-wasi \
            --with-static-linked-ext \
            LDFLAGS=" \
              -Xlinker --stack-first \
              -Xlinker -z -Xlinker stack-size=16777216 \
            " \
            optflags="${{ matrix.entry.optflags }}" \
            debugflags="${{ matrix.entry.debugflags }}" \
            wasmoptflags="${{ matrix.entry.wasmoptflags }} ${{ matrix.entry.debugflags }}"

      # miniruby may not be built when cross-compling
      - run: make mini ruby
      - name: Run basictest
        run: wasmtime run ./../build/miniruby --mapdir /::./ -- basictest/test.rb
        working-directory: src
      - name: Run bootstraptest (no thread)
        run: |
          NO_THREAD_TESTS="$(grep -L Thread -R ./bootstraptest | awk -F/ '{ print $NF }' | uniq | sed -n 's/test_\(.*\).rb/\1/p' | paste -s -d, -)"
          ruby ./bootstraptest/runner.rb --ruby="$(which wasmtime) run $PWD/../build/ruby --mapdir /::./ -- " --verbose "--sets=$NO_THREAD_TESTS"
        working-directory: src

      - uses: ruby/action-slack@0bd85c72233cdbb6a0fe01d37aaeff1d21b5fce1 # v3.2.1
        with:
          payload: |
            {
              "ci": "GitHub Actions",
              "env": "${{ github.workflow }} / ${{ matrix.name }}",
              "url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
              "commit": "${{ github.sha }}",
              "branch": "${{ github.ref_name }}"
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SIMPLER_ALERTS_URL }} # ruby-lang slack: ruby/simpler-alerts-bot
        if: ${{ failure() && github.event_name == 'push' }}

defaults:
  run:
    working-directory: build
